package kr.hkit.androidcomplete03.ETC;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SecondActivity extends Activity {
	Button closeBtn;
	int value = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_secondactivity);

		Intent inIntent = getIntent();

		String re = inIntent.getStringExtra("re"); // 라디오버튼으로 클릭한거 가져와서 계산하는 소스
		
		if (re.equals("+")) {
			value = inIntent.getIntExtra("Num1", 0) + inIntent.getIntExtra("Num2", 0);
		} else if (re.equals("-")) {
			value = inIntent.getIntExtra("Num1", 0) - inIntent.getIntExtra("Num2", 0);
		} else if (re.equals("*")) {
			value = inIntent.getIntExtra("Num1", 0) * inIntent.getIntExtra("Num2", 0);
		} else if (re.equals("/")) {
			try {
				value = inIntent.getIntExtra("Num1", 0) / inIntent.getIntExtra("Num2", 0);
			} catch(ArithmeticException e) {
				Toast.makeText(getApplicationContext(), "0으로 나눌수 없음!", 0).show();
			}
		}

		closeBtn = (Button) findViewById(R.id.btnReturn);

		closeBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent outIntent = new Intent(getApplicationContext(), TestActivity.class);
				outIntent.putExtra("result", value);
				setResult(RESULT_OK, outIntent);
				finish();
			}
		});

	}
}
